package com.tony.blog.api.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: PageUtil
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/15/21:15
 * @Description: 分页通用类：也可以指定long，一般int就够
 */
@Data
@ToString
public class PageUtil implements Serializable {

    private static final long serialVersionUID = 1L;

    //总记录数
    private int totalCount;

    //每页记录数
    private int pageSize;

    //总页数
    private int totalPage;

    //当前第几页
    private int currPage;

    //列表数据
    private List<?> list;

    /**
    * @Description: PageUtil 分页
    * @Param: [list 列表数据, totalCount 总记录数, pageSize 每页记录数, currPage 当前页数]
    * @return:
    * @Author: 皮蛋布丁
    * @Date: 2021/7/15 21:24
    */
    public PageUtil( List<?> list, int totalCount, int pageSize, int currPage) {
        this.list = list;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int) Math.ceil((double) totalCount/pageSize); //Math.ceil():返回大于等于数字参数的最小整数(取整函数)，对数字进行上舍入
    }

    /**
    * @Description: PageUtil 分页
    * @Param: [page]
    * @return:
    * @Author: 皮蛋布丁
    * @Date: 2021/7/15 21:26
    */
    public PageUtil(IPage<?> page) {
        this.list = page.getRecords();
        this.totalCount = (int) page.getTotal();
        this.pageSize = (int) page.getSize();
        this.currPage = (int) page.getCurrent();
        this.totalPage = (int) page.getPages();
    }
}
